package com.kelab.dify;

import com.kelab.dify.config.DifyConfig;
import com.kelab.dify.service.*;
import com.kelab.dify.exception.DifyException;
import okhttp3.OkHttpClient;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.TimeUnit;

/**
 * Dify API客户端主类
 * 提供对Dify所有API功能的访问
 * 
 * @author kelab
 * @version 1.0
 */
public class DifyClient {
    
    private static final Logger logger = LoggerFactory.getLogger(DifyClient.class);
    
    private final DifyConfig config;
    private final OkHttpClient httpClient;
    private final ObjectMapper objectMapper;
    
    // 各种服务实例
    private final ChatService chatService;
    private final WorkflowService workflowService;
    private final KnowledgeBaseService knowledgeBaseService;
    private final AudioService audioService;
    private final FileService fileService;
    private final ApplicationService applicationService;
    
    /**
     * 构造函数
     * 
     * @param config Dify配置
     */
    public DifyClient(DifyConfig config) {
        this.config = config;
        this.objectMapper = createObjectMapper();
        this.httpClient = createHttpClient();
        
        // 初始化各种服务
        this.chatService = new ChatService(this.httpClient, this.objectMapper, this.config);
        this.workflowService = new WorkflowService(this.httpClient, this.objectMapper, this.config);
        this.knowledgeBaseService = new KnowledgeBaseService(this.httpClient, this.objectMapper, this.config);
        this.audioService = new AudioService(this.httpClient, this.objectMapper, this.config);
        this.fileService = new FileService(this.httpClient, this.objectMapper, this.config);
        this.applicationService = new ApplicationService(this.httpClient, this.objectMapper, this.config);
        
        logger.info("DifyClient initialized with base URL: {}", config.getBaseUrl());
    }
    
    /**
     * 创建HTTP客户端
     */
    private OkHttpClient createHttpClient() {
        return new OkHttpClient.Builder()
                .connectTimeout(config.getConnectTimeout(), TimeUnit.MILLISECONDS)
                .readTimeout(config.getReadTimeout(), TimeUnit.MILLISECONDS)
                .writeTimeout(config.getWriteTimeout(), TimeUnit.MILLISECONDS)
                .build();
    }
    
    /**
     * 创建JSON映射器
     */
    private ObjectMapper createObjectMapper() {
        ObjectMapper mapper = new ObjectMapper();
        // 配置JSON处理选项
        mapper.configure(com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        return mapper;
    }
    
    // Getter方法提供对各种服务的访问
    
    /**
     * 获取聊天服务
     * 
     * @return ChatService实例
     */
    public ChatService chat() {
        return chatService;
    }
    
    /**
     * 获取工作流服务
     * 
     * @return WorkflowService实例
     */
    public WorkflowService workflow() {
        return workflowService;
    }
    
    /**
     * 获取知识库服务
     * 
     * @return KnowledgeBaseService实例
     */
    public KnowledgeBaseService knowledgeBase() {
        return knowledgeBaseService;
    }
    
    /**
     * 获取音频服务
     * 
     * @return AudioService实例
     */
    public AudioService audio() {
        return audioService;
    }
    
    /**
     * 获取文件服务
     * 
     * @return FileService实例
     */
    public FileService file() {
        return fileService;
    }
    
    /**
     * 获取应用服务
     * 
     * @return ApplicationService实例
     */
    public ApplicationService application() {
        return applicationService;
    }
    
    /**
     * 获取配置信息
     * 
     * @return DifyConfig实例
     */
    public DifyConfig getConfig() {
        return config;
    }
    
    /**
     * 关闭客户端，释放资源
     */
    public void close() {
        if (httpClient != null) {
            httpClient.dispatcher().executorService().shutdown();
            httpClient.connectionPool().evictAll();
        }
        logger.info("DifyClient closed");
    }
}